package NsingCHM.Video;

/** 
 Video ��ժҪ˵����
*/
public class Video
{
	public Video()
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//
	}

	public static class video
	{
		public String MeconderPath = MOLE.GetValue.GetConfigString("ffmpegpath") + "mencoder.exe";
		public String FlvmdiPath = MOLE.GetValue.GetConfigString("ffmpegpath") + "flvmdi.exe";
		public String FfmpegPath = MOLE.GetValue.GetConfigString("ffmpegpath") + "ffmpeg.exe";
		public String VideoPath = MOLE.GetValue.GetConfigString("videopath");
		public String FlvPath = MOLE.GetValue.GetConfigString("flvpath");
		public String ImgPath = MOLE.GetValue.GetConfigString("imgpath");
		public String FileImg = "";
		public String FileVideo = "";
		public String FileFlv = "";
		public String PathLog = MOLE.GetValue.GetConfigString("logpath") + MOLE.comm.getDate() + ".txt";

		public String VideoId = "";

		public static String MeconderPara = "";
		public static String FlvmdiPara = "";
		public static String FfmpegPara = "";



//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region  processing //windwos service.


		public final void main()
		{
			for (int i = 0;i < 100;i++)
			{
				try
				{
					go();
					MOLE.file.write(PathLog, "\r\n " + java.time.LocalDateTime.now().toString() + "  Process over ,slepp  5 secs,this time is :" + String.valueOf(i));
					Thread.sleep(5000);

				}
				catch (RuntimeException ee)
				{
					MOLE.file.write(PathLog, "\r\n" + java.time.LocalDateTime.now().toString() + "  Errrrrrr ....." + ee.toString());
					Thread.sleep(5000);

				}
			}

		}



		public final void go()
		{


			//get video info
			GetVideoName();
			if (VideoId.equals(""))
			{
				MOLE.file.write(PathLog, "\r\n" + java.time.LocalDateTime.now().toString() + "  No video to flv,exit  ");
				//	SetStatuOver();
				return;
			}
			//update video converStatue is processing..
			UpdateVideotState(Integer.parseInt(VideoId), 2);

			convertFixflvImging();


			//update video converStatue after converted
			if ((new java.io.File(FileFlv)).isFile() && (new java.io.File(FileImg)).isFile())
			{
				UpdateVideotState(Integer.parseInt(VideoId), 1);
			}
			else
			{
				UpdateVideotState(Integer.parseInt(VideoId), 9);
			}


		}


		public final void convertFixflvImging()
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#region
			if (!Path.GetExtension(FileVideo).equals(".flv"))
			{
				//converting
				MOLE.file.write(PathLog, "  ");
				MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  convert starting  .....");
				convert();
				MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  convert  para:(start)" + MOLE.video.MeconderPara + "(end)");
				MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + " convert   end .....");
			}


			//fixing
			MOLE.file.write(PathLog, "  ");
			MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  fix  starting  .....");
			fixflv();
			MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  fix  para:(start)" + MOLE.video.FlvmdiPara + "(end)");
			MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  fix   end .....");


			//imging
			MOLE.file.write(PathLog, "  ");
			MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  imging starting  .....");
			getFlvImg();
			MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  imging  para:(start)" + MOLE.video.FfmpegPara + "(end)");
			MOLE.file.write(PathLog, java.time.LocalDateTime.now().toString() + "  imging  end .....");
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#endregion
		}


		public final void GetVideoName()
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#region
			//2:converting  1:cov ok  9: conv fail
			String sql = "select top 1 * from sms_videox where ConvertState = 0 or   ConvertState = 2 order by videoid asc";
			String vid = "";
			String vname = "";
			String vnameNoExd = "";
			DataTable dt = MOLE.SQL.GetDataTable(sql);
			if (dt.Rows.size() > 0)
			{
				vid = dt.Rows[0]["videoid"].toString();
				vname = dt.Rows[0]["FilePath"].toString() + dt.Rows[0]["SourceExtend"].toString();
				vnameNoExd = dt.Rows[0]["FilePath"].toString();
			}

			VideoId = vid;
			FileVideo = VideoPath + vname;
			FileFlv = FlvPath + vnameNoExd + ".flv";
			FileImg = ImgPath + vnameNoExd + ".jpg";

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#endregion
		}


		public final boolean UpdateVideotState(int vid, int state)
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#region
			String sql = "update sms_videox set ConvertState=" + state + " where videoid=" + String.valueOf(vid);
			MOLE.SQL.ExecuteSql(sql);
			return true;
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#endregion
		}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion




		public final boolean convert()
		{
			convert(MeconderPath, FileVideo, FileFlv);
			return true;
		}
		public final boolean fixflv()
		{
			fixflv(FlvmdiPath, FileFlv);
			return true;

		}
		public final boolean getFlvImg()
		{
			getFlvImg(FfmpegPath, FileFlv, FileImg);
			return true;
		}






		public static void ConvertFixThumb(String mencoderPath, String flvFixpath, String ffmpegPath, String avi, String flv, String img)
		{
			convert(mencoderPath, avi, flv);
			fixflv(flvFixpath, flv);
			getFlvImg(ffmpegPath, flv, img);


		}
		public static void ConvertFixThumb(String flvFixpath, String ffmpegPath, String flv, String img)
		{
			//convert(mencoderPath,avi,flv);
			fixflv(flvFixpath, flv);
			getFlvImg(ffmpegPath, flv, img);


		}


		public static boolean convert(String exepath, String inFile, String outFile)
		{

			System.Diagnostics.ProcessStartInfo o = new System.Diagnostics.ProcessStartInfo();
			System.Diagnostics.Process Proc;

			//convert
			String exeFlvPara = inFile + " -oac mp3lame -lameopts br=32 -ovc lavc -lavcopts vcodec=flv:vbitrate=400" + "  -srate 22050 -ofps 15 -vf scale=400:-3 " + " -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames " + " -ffourcc FLV1 -of lavf -sws 3  -o " + outFile;
			MeconderPara = exepath + "  " + exeFlvPara;
			//	 -oac mp3lame -lameopts br=32 -ovc lavc -lavcopts vcodec=flv:vbitrate=240 -srate 22050 -ofps 15 -vf scale=400:-3 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -ffourcc FLV1 -of lavf -o D:\myapps\videoupload\streams\_definst_\20072101548244.flv

			o.FileName = exepath;
			o.Arguments = exeFlvPara;
			Proc = System.Diagnostics.Process.Start(o);

			while (!Proc.HasExited)
			{
				Thread.sleep(2000);
			}
			Thread.sleep(2000);
			return true;

		}



		public static boolean fixflv(String exepath, String flvfile)
		{
			System.Diagnostics.ProcessStartInfo o = new System.Diagnostics.ProcessStartInfo();
			System.Diagnostics.Process Proc;
			String para = flvfile + "  /s /e /k ";
			FlvmdiPara = exepath + " " + para;

			o.FileName = exepath;
			o.Arguments = para;
			Proc = System.Diagnostics.Process.Start(o);

			while (!Proc.HasExited)
			{
				Thread.sleep(2000);
			}
			Thread.sleep(2000);
			return true;

		}



		public static boolean getFlvImg(String exepath, String flvfile, String outImg)
		{
			System.Diagnostics.ProcessStartInfo o = new System.Diagnostics.ProcessStartInfo();
			System.Diagnostics.Process Proc;
			String para = "  -i " + flvfile + " -ss 1 -vframes 1 -r 1 -ac 1 -ab 2  -f image2 " + outImg;
			para = "  -i " + flvfile + " -y -f image2 -ss 8 -t 0.001 -s 120x90 " + outImg;
			//			 info2.Arguments = " -i " + text11 + " -y -f image2 -ss 8 -t 0.001 -s 120x90 " + text7;
			FfmpegPara = exepath + "  " + para;

			o.FileName = exepath;
			o.Arguments = para;
			Proc = System.Diagnostics.Process.Start(o);
			while (!Proc.HasExited)
			{
				Thread.sleep(2000);
			}
			Thread.sleep(2000);
			return true;

		}

	}
}